package com.pango.system.services.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pango.core.utils.BeanUtils;
import com.pango.system.entity.OrganizationEntity;
import com.pango.system.mapper.OrganizationMapper;
import com.pango.system.model.Organization;
import com.pango.system.services.OrganizationService;

@Service
public class OrganizationServiceImpl implements OrganizationService{
	
	@Autowired
	private OrganizationMapper organizationMapper;

	@Override
	public List<Organization> getAll() {
		// TODO Auto-generated method stub
		return organizationMapper.selectAll();
	}

	@Override
	public List<OrganizationEntity> selectOrganizationByParentId(Long parantId) {
		// TODO Auto-generated method stub
		return organizationMapper.selectOrganizationByParentId(parantId);
	}

	@Override
	public List<OrganizationEntity> selectOrganizationNoSelf(Organization organization) {
		return organizationMapper.selectOrganizationNoSelf(organization);
	}

	@Override
	public OrganizationEntity getEntityById(Long id) {
		OrganizationEntity entity = new OrganizationEntity();
		Organization org = this.organizationMapper.selectByPrimaryKey(id);
		BeanUtils.copyProperties(entity, org);
		if(null != org &&null != org.getParentId()) {
			Organization parent = this.organizationMapper.selectByPrimaryKey(org.getParentId());
			//entity.setParent(parent);
		}
		return entity;
	}

	@Override
	public void saveEntity(OrganizationEntity entity) {
		Organization org  = new Organization();
		BeanUtils.copyProperties(org, entity);
		// TODO Auto-generated method stub
		if(entity.getParent() != null) {
			org.setParentId(entity.getParent().getId());
		}
		if(org.getId() != null) {
			this.organizationMapper.updateByPrimaryKey(org);
		} else {
			this.organizationMapper.insert(org);
		}
		
	}
}
